package com.bluegalaxy.sitescore.controller;

import com.bluegalaxy.sitescore.entity.Soil;
import com.bluegalaxy.sitescore.entity.TbDangerous;
import com.bluegalaxy.sitescore.entity.TbSiteInfo;
import com.bluegalaxy.sitescore.entity.TbSoilExposure;
import com.bluegalaxy.sitescore.service.ITbDangerousService;
import com.bluegalaxy.sitescore.service.ITbSoilExposureService;
import com.bluegalaxy.sitescore.service.SoilService;
import com.bluegalaxy.sitescore.service.TbSiteSourceService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by LZRuil on 2017/11/29.
 */
@Controller
@RequestMapping("/soil")
public class SoilController extends BaseController  {
    @Autowired
    private SoilService soilService;

    @Autowired
    private ITbSoilExposureService iTbSoilExposureService;

    @Autowired
    private ITbDangerousService tbDangerousService;

    @Autowired
    private TbSiteSourceService tbSiteSourceService;


    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping("/add/{siteId}")
    public String list(Model model,@PathVariable int siteId) {
        int id =siteId;
        if(iTbSoilExposureService.getCountById(id)!=0) {
            model.addAttribute("soil", iTbSoilExposureService.selectById(id));
        }else {
            model.addAttribute("soil", new TbSoilExposure());
        }
        List<TbDangerous> dangerousList = tbDangerousService.getDangerousSelected(id);
        model.addAttribute("dangerouSelect",toJson(dangerousList));
        model.addAttribute("observable",tbSiteSourceService.isObservable(id));
        model.addAttribute("siteId",siteId);
        return "/soil/add";
    }


    @com.baomidou.kisso.annotation.Permission("1001")    @RequestMapping("/surface{id}")
    public String surface(Model model,@PathVariable String id) {
        return "/soil/surface"+id;
    }






    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping(value = "/caculateScore",method = RequestMethod.POST)
    public @ResponseBody String soilCaculate(TbSoilExposure soil, Model model){
        //未获取
        int id =soil.getSource_id();
       // TbSoilExposure tbSoilExposure = new TbSoilExposure();
        Date date = new Date();
        int exposureInt = 0;
        int attractAccess= 0;
        int areasOfContamination= 0;
        int nearbyIndividual= 0;
        int popWith= 0;
        int residentIndividual= 0;
        int popConcent1= 0;
        int popConcent2= 0;
        int worker= 0;
        int resources= 0;
        int terrestrialSensEnvion= 0;
        BigDecimal uncappedScore = null;
        float score = 0;
        /*if (soil.getLikelihood_of_exposure()!=550&&soil.getLikelihood_of_exposure()!=0){
            System.out.println(soil.getLikelihood_of_exposure());
            model.addAttribute("tipMsg","信息1输入错误");
         //   return  "/soil/add";
            return "1";
        }else {
            exposureInt = soil.getLikelihood_of_exposure();
        }
        if (soil.getResident_individual()!=0&&soil.getResident_individual()!=45&&soil.getResident_individual()!=50){
            model.addAttribute("tipMsg","信息5输入错误");
           // return  "/soil/add";
            return "5";
        }else {
            residentIndividual = soil.getResident_individual();
        }
        if (soil.getPop_concent1()<0){
            model.addAttribute("tipMsg","信息6a输入错误");
          //  return  "/soil/add";
            return "3";
        }else {
            popConcent1 = soil.getPop_concent1();
        }
        if (soil.getPop_concent2()<0){
            model.addAttribute("tipMsg","信息6b输入错误");
          //  return  "/soil/add";
            return "4";
        }else {
            popConcent2 = soil.getPop_concent2();
        }
        if (soil.getWorker()!=0&&soil.getWorker()!=5&&soil.getWorker()!=10&&soil.getWorker()!=15){
            model.addAttribute("tipMsg","信息7输入错误");
          //  return  "/soil/add";
            return "5";
        }else {
            worker = soil.getWorker();
        }
        if (soil.getResources()!=0&&soil.getResources()!=5){
            model.addAttribute("tipMsg","信息8输入错误");
           // return  "/soil/add";
            return "6";
        }else {
            resources = soil.getResources();
        }
        if (soil.getTerrestrial_sens_envion()!=0&&soil.getTerrestrial_sens_envion()!=25&&soil.getTerrestrial_sens_envion()!=50&&soil.getTerrestrial_sens_envion()!=75&&soil.getTerrestrial_sens_envion()!=100){
            model.addAttribute("tipMsg","信息9输入错误");
           // return  "/soil/add";
            return "7";
        }else {
            terrestrialSensEnvion = soil.getTerrestrial_sens_envion();
        }
        if (soil.getAttract_access()!=0&&soil.getAttract_access()!=5&&soil.getAttract_access()!=10&&soil.getAttract_access()!=25&&soil.getAttract_access()!=50&&soil.getAttract_access()!=75&&soil.getAttract_access()!=100){
            model.addAttribute("tipMsg","信息12输入错误");
           // return  "/soil/add";
            return "8";
        }else {
            attractAccess = soil.getAttract_access();
        }
        if (soil.getAreas_of_contamination()!=5&&soil.getAreas_of_contamination()!=20&&soil.getAreas_of_contamination()!=40&&soil.getAreas_of_contamination()!=60&&soil.getAreas_of_contamination()!=80&&soil.getAreas_of_contamination()!=100){
            model.addAttribute("tipMsg","信息13输入错误");
           // return  "/soil/add";
            return "9";
        }else {
            areasOfContamination = soil.getAreas_of_contamination();
        }
        if (soil.getNearby_individual()!=0&&soil.getNearby_individual()!=1){
            model.addAttribute("tipMsg","信息18输入错误");
           // return  "/soil/add";
            return "10";
        }else {
            nearbyIndividual = soil.getNearby_individual();
        }
        if (soil.getPop_with()<0){
            model.addAttribute("tipMsg","信息19输入错误");
           // return  "/soil/add";
            return "11";
        }else {
            popWith = soil.getPop_with();
        }
*/
        if(!soilService.judgeData(soil)){
            return "false";
        }
        exposureInt = soil.getLikelihood_of_exposure();
        residentIndividual = soil.getResident_individual();
        popConcent1 = soil.getPop_concent1();
        popConcent2 = soil.getPop_concent2();
        worker = soil.getWorker();
        resources = soil.getResources();
        terrestrialSensEnvion = soil.getTerrestrial_sens_envion();
        attractAccess = soil.getAttract_access();
        areasOfContamination = soil.getAreas_of_contamination();
        nearbyIndividual = soil.getNearby_individual();
        popWith = soil.getPop_with();


        int wc = soil.getWaste_characteristics();
        BigDecimal pop = new BigDecimal(popConcent1).multiply(
                new BigDecimal(10)).add(
                new BigDecimal(popConcent2));
     //   System.out.println("pop:"+pop);
        BigDecimal residentT = new BigDecimal(residentIndividual).add(pop).add(
                new BigDecimal(worker)).add(
                new BigDecimal(resources)).add(
                new BigDecimal(terrestrialSensEnvion));
    //   System.out.println("residentT:"+residentT);
        //计算附近T
        BigDecimal nearbyT = new BigDecimal(nearbyIndividual).add(new BigDecimal(popWith).multiply(new BigDecimal(0.1)));
     //   System.out.println("nearbyT:"+nearbyT);
        int lr = soilService.CaculateLR(attractAccess,areasOfContamination);
      //  System.out.println("lr:"+lr);
        BigDecimal nearby = soilService.CaculateNearby(lr,wc,nearbyT);
      //  System.out.println("nearby:"+nearby);
        BigDecimal resident =  soilService.CaculateResident(exposureInt,wc,residentT);
   //     System.out.println("resident:"+resident);
        //计算最终成绩
        uncappedScore = soilService.CaculateScore(resident ,nearby);
     //  System.out.println("uncappedScore:"+uncappedScore);
       if(uncappedScore.floatValue()>100){
           score = 100;
       }else {
           score = uncappedScore.floatValue();
       }
        soil.setSoil_exp_score(score);
        soil.setSource_id(id);
        soil.setToxicityId(1);
      //  soil.setWaste_characteristics(1);
        soil.setToxicityId1(1);
      //  soil.setWaste_characteristics1(1);
        soil.setCreate_time(date);
        soil.setUpdate_time(date);
        soil.setIs_delete(1);
      //  System.out.println(soil.getId());
        if(iTbSoilExposureService.getCountById(id)==0) {
            if (! iTbSoilExposureService.insertTbSoilExposure(soil)){
                return "false";
            }
        }else{
            soil.setId(id);
            if (! iTbSoilExposureService.updateTbSoilExposure(soil)){
                return "false";
            }
        }



        model.addAttribute("score",uncappedScore);
        return  String.valueOf(score);
    }

}
